In [1]:
import pandas as pd
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
In [2]:
from keras.datasets import mnist
In [3]:
(X_train, y_train), (X_test, y_test) = mnist.load_data('/tmp/mnist.npz')
In [4]:
X_train.shape
Out[4]:
In [5]:
X_test.shape
Out[5]:
In [6]:
X_train[0]
Out[6]:
In [7]:
plt.imshow(X_train[0],
cmap = 'gray')
Out[7]:
In [8]:
# Flattening the input
X_train = X_train.reshape(-1, 28 * 28)
X_test = X_test.reshape(-1, 28 * 28)
In [9]:
X_train.shape
Out[9]:
In [10]:
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255.0
X_test /= 255.0
In [11]:
X_train[0]
Out[11]:
In [12]:
from keras.utils.np_utils import to_categorical
In [13]:
y_train_cat = to_categorical(y_train)
y_test_cat = to_categorical(y_test)
In [14]:
y_train[0]
Out[14]:
In [15]:
# One-hot encoded labels
y_train_cat[0]
Out[15]:
In [16]:
y_train_cat.shape
Out[16]:
In [17]:
y_test_cat.shape
Out[17]:
In [18]:
from keras.models import Sequential
from keras.layers import Dense
import keras.backend as K
K.clear_session()
model = Sequential()
model.add(Dense(10,
input_dim = 28 * 28,
activation = 'relu'))
model.add(Dense(256,
activation = 'relu'))
'''
model.add(Dense(128,
activation = 'relu'))
'''
model.add(Dense(16,
activation = 'relu'))
model.add(Dense(10,
activation = 'softmax'))
model.compile(loss = 'categorical_crossentropy',
optimizer = 'rmsprop',
metrics = ['accuracy'])
In [19]:
h = model.fit(X_train,
y_train_cat,
batch_size = 12,
epochs = 10,
verbose = 2,
validation_split = 0.3)
In [20]:
plt.figure(figsize = (12,7))
plt.plot(h.history['acc'])
plt.plot(h.history['val_acc'])
plt.legend(['Training', 'Validation'], loc = 'lower right')
plt.title('Accuracy')
plt.xlabel('Epochs')
Out[20]:
In [21]:
test_accuracy = model.evaluate(X_test, y_test_cat)[1]
# Printing the accuracy
test_accuracy * 100
Out[21]:
In [22]:
A = np.random.randint(10, size = (2, 3, 4, 5))
B = np.random.randint(10, size = (2, 3))
In [23]:
A
Out[23]:
In [24]:
A[0, 1, 0, 3]
Out[24]:
In [25]:
B
Out[25]:
In [26]:
img = np.random.randint(255,
size = (4, 4, 3),
dtype = 'uint8')
img
Out[26]:
In [27]:
plt.figure(figsize = (10, 7))
plt.subplot(221)
plt.imshow(img)
plt.title("All Channels combined")
plt.subplot(222)
plt.imshow(img[:, : , 0],
cmap = 'Reds')
plt.title("Red channel")
plt.subplot(223)
plt.imshow(img[:, : , 1],
cmap = 'Greens')
plt.title("Green channel")
plt.subplot(224)
plt.imshow(img[:, : , 2],
cmap = 'Blues')
plt.title("Blue channel")
Out[27]:
In [28]:
2 * A
Out[28]:
In [29]:
A + A
Out[29]:
In [30]:
A.shape
Out[30]:
In [31]:
B.shape
Out[31]:
In [32]:
np.tensordot(A,
B,
axes = ([0, 1], [0, 1]))
Out[32]:
In [33]:
np.tensordot(A,
B,
axes = ([0], [0])).shape
Out[33]:
In [34]:
a = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
dtype='float32')
In [35]:
b = np.array([-1, 1],
dtype='float32')
In [36]:
c = np.convolve(a, b)
In [37]:
a
Out[37]:
In [38]:
b
Out[38]:
In [39]:
c
Out[39]:
In [40]:
plt.subplot(211)
plt.plot(a, 'o-')
plt.subplot(212)
plt.plot(c, 'o-')
Out[40]:
In [41]:
from scipy.ndimage.filters import convolve
from scipy.signal import convolve2d
from scipy import misc
In [42]:
img = misc.ascent()
In [43]:
img.shape
Out[43]:
In [44]:
plt.imshow(img,
cmap = 'gray')
Out[44]:
In [45]:
h_kernel = np.array([[ 1, 2, 1],
[ 0, 0, 0],
[-1, -2, -1]])
In [46]:
plt.imshow(h_kernel,
cmap = 'gray')
Out[46]:
In [47]:
res = convolve2d(img, h_kernel)
plt.imshow(res,
cmap = 'gray')
Out[47]:
In [48]:
from keras.layers import Conv2D
In [49]:
img.shape
Out[49]:
In [50]:
plt.figure(figsize = (7, 7))
plt.imshow(img,
cmap = 'gray')
Out[50]:
In [51]:
img_tensor = img.reshape((1, 512, 512, 1))
In [52]:
model = Sequential()
model.add(Conv2D(filters = 1,
kernel_size = (3, 3),
strides = (2,1),
input_shape = (512, 512, 1)))
model.compile('adam', 'mse')
In [53]:
img_pred_tensor = model.predict(img_tensor)
In [54]:
img_pred_tensor.shape
Out[54]:
In [55]:
img_pred = img_pred_tensor[0, :, :, 0]
In [56]:
plt.imshow(img_pred,
cmap = 'gray')
Out[56]:
In [57]:
weights = model.get_weights()
In [58]:
weights[0].shape
Out[58]:
In [59]:
plt.imshow(weights[0][:, :, 0, 0],
cmap = 'gray')
Out[59]:
In [60]:
weights[0] = np.ones(weights[0].shape)
In [61]:
model.set_weights(weights)
In [62]:
img_pred_tensor = model.predict(img_tensor)
In [63]:
img_pred = img_pred_tensor[0, :, :, 0]
In [64]:
plt.imshow(img_pred,
cmap = 'gray')
Out[64]:
In [65]:
model = Sequential()
model.add(Conv2D(filters = 1,
kernel_size = (3, 3),
input_shape = (512, 512, 1),
padding='same'))
model.compile('adam', 'mse')
img_pred_tensor = model.predict(img_tensor)
img_pred_tensor.shape
Out[65]:
In [66]:
img_pred_tensor = img_pred_tensor[0, :, :, 0]
plt.imshow(img_pred_tensor,
cmap = 'gray')
Out[66]:
In [67]:
from keras.layers import MaxPool2D, AvgPool2D
In [68]:
model = Sequential()
model.add(MaxPool2D(pool_size = (5, 5),
input_shape = (512, 512, 1)))
model.compile('adam', 'mse')
In [69]:
img_pred = model.predict(img_tensor)[0, :, :, 0]
In [70]:
plt.imshow(img_pred,
cmap = 'gray')
Out[70]:
In [71]:
model = Sequential()
model.add(AvgPool2D(pool_size = (5, 5),
input_shape = (512, 512, 1)))
model.compile('adam', 'mse')
In [72]:
img_pred = model.predict(img_tensor)[0, :, :, 0]
plt.imshow(img_pred,
cmap = 'gray')
Out[72]:
In [73]:
X_train = X_train.reshape(-1, 28, 28, 1)
X_test = X_test.reshape(-1, 28, 28, 1)
In [74]:
X_train.shape
Out[74]:
In [75]:
from keras.layers import Flatten, Activation
In [76]:
K.clear_session()
model = Sequential()
model.add(Conv2D(8,
(3, 3),
input_shape = (28, 28, 1)))
model.add(MaxPool2D(pool_size = (2, 2)))
model.add(Activation('relu'))
model.add(Flatten())
model.add(Dense(32,
activation = 'relu'))
model.add(Dense(10,
activation = 'softmax'))
model.compile(loss = 'categorical_crossentropy',
optimizer = 'rmsprop',
metrics = ['accuracy'])
In [77]:
model.summary()
In [78]:
model.fit(X_train,
y_train_cat,
batch_size = 12,
epochs = 2,
verbose = 2,
validation_split = 0.3)
Out[78]:
In [79]:
evaluated = model.evaluate(X_test, y_test_cat)
In [80]:
evaluated[1] * 100
Out[80]:
You've been hired by a shipping company to overhaul the way they route mail, parcels and packages. They want to build an image recognition system capable of recognizing the digits in the zipcode on a package, so that it can be automatically routed to the correct location. You are tasked to build the digit recognition system. Luckily, you can rely on the MNIST dataset for the intial training of your model!
Build a deep convolutional neural network with at least two convolutional and two pooling layers before the fully connected layer.
Conv2D
layer after the first MaxPool2D
, give it 64 filters.MaxPool2D
after that oneActivation
layer
In [81]:
X_train.shape, X_test.shape, y_train_cat.shape, y_test_cat.shape
Out[81]:
In [82]:
from keras.layers import Input, Dense, Conv2D, MaxPool2D, Activation, Flatten
import keras.backend as K
from keras.models import Model
from keras.optimizers import Adam
In [86]:
K.clear_session()
inp = Input(shape = (28, 28, 1 ))
net = Conv2D(filters = 64,
kernel_size = (2, 2),
activation = 'relu',
padding = 'valid')(inp)
net = MaxPool2D(pool_size = (2, 2),
strides = (2, 2),
padding = 'valid')(net)
net = Conv2D(filters = 8,
kernel_size = (2, 2),
activation = 'relu',
padding = 'valid')(net)
net = MaxPool2D(pool_size = (2, 2),
strides = (2, 2),
padding = 'valid')(net)
net = Activation(activation = 'relu')(net)
net = Flatten()(net)
prediction = Dense(10, activation = 'softmax')(net)
In [87]:
model = Model(inputs = inp, outputs = prediction)
model.compile(optimizer = Adam(),
loss = 'categorical_crossentropy',
metrics = ['accuracy'])
In [88]:
model.summary()
In [89]:
model.fit(X_train,
y_train_cat,
batch_size = 50,
validation_split = 0.2,
epochs = 5,
verbose = 2)
Out[89]:
Pleased with your performance with the digits recognition task, your boss decides to challenge you with a harder task. Their online branch allows people to upload images to a website that generates and prints a postcard that is shipped to destination. Your boss would like to know what images people are loading on the site in order to provide targeted advertising on the same page, so he asks you to build an image recognition system capable of recognizing a few objects. Luckily for you, there's a dataset ready made with a collection of labeled images. This is the Cifar 10 Dataset, a very famous dataset that contains images for 10 different categories:
In this exercise we will reach the limit of what you can achieve on your laptop and get ready for the next session on cloud GPUs.
Here's what you have to do:
keras.datasets.cifar10.load_data()
In [138]:
from keras.datasets import cifar10
In [139]:
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
In [140]:
X_train.shape
Out[140]:
In [141]:
X_train[0, :, :, 0]
Out[141]:
In [142]:
X_train[0, :, :, 1]
Out[142]:
In [143]:
X_train[0, :, :, 2]
Out[143]:
In [144]:
np.max(X_train[0, :, :, :])
Out[144]:
In [145]:
np.min(X_train[0, :, :, :])
Out[145]:
In [146]:
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train = X_train / 255.0
X_test = X_test / 255.0
In [147]:
from keras.utils import to_categorical
y_train_cat = to_categorical(y_train)
y_test_cat = to_categorical(y_test)
In [148]:
y_train_cat[:5]
Out[148]:
In [149]:
print(y_train_cat.shape)
In [150]:
y_test_cat[:5]
Out[150]:
In [151]:
print(y_test_cat.shape)
In [172]:
'''conv2d
conv2d
maxpool
conv2d
conv2d
maxpool
flatten
dense
output
'''
from keras.models import Model
from keras.layers import Input, Dense, Conv2D, MaxPool2D, Flatten
from keras.regularizers import l2
# Inputs 32 x 32 x 3
inp = Input(shape = (32, 32, 3))
net = Conv2D(filters = 8,
kernel_size = (2, 2),
padding = 'same')(inp)
net = Conv2D(filters = 8,
kernel_size = (2, 2),
padding = 'same')(net)
net = MaxPool2D(pool_size = (2, 2), padding = 'valid')(net)
net = Conv2D(filters = 8,
kernel_size = (2, 2),
padding = 'same')(net)
net = Conv2D(filters = 8,
kernel_size = (2, 2),
padding = 'same')(net)
net = MaxPool2D(pool_size = (2, 2), padding = 'valid')(net)
net = Flatten()(net)
net = Dense(units = 10,
activation = 'relu')(net)
prediction = Dense(units = 10,
activation = 'softmax')(net)
In [181]:
model = Model(inputs = [inp], outputs = [prediction])
In [182]:
from keras.optimizers import Adam
In [188]:
model.compile(optimizer = Adam(),
loss = 'categorical_crossentropy',
metrics = ['accuracy'])
In [189]:
model.summary()
In [190]:
model.fit(X_train,
y_train_cat,
batch_size = 50,
validation_split = 0.2,
epochs = 5,
verbose = 2)
Out[190]: